package me.zhengjie.modules.web.rest.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.PageData;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.course.StudentWitnessCategoryResp;
import me.zhengjie.modules.web.domain.course.StudentWitnessLikeReq;
import me.zhengjie.modules.web.domain.course.StudentWitnessListReq;
import me.zhengjie.modules.web.domain.course.StudentWitnessListResp;
import me.zhengjie.modules.web.service.course.IStudentWitnessService;
import me.zhengjie.request.ATMRContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "前端接口-学员见证")
@RequestMapping("web/witness")
public class StudentWitnessController {

    @Autowired
    private IStudentWitnessService studentWitnessService;

    @PostMapping("category")
    @ApiOperation("前端接口-学员见证-分类")
    public ResponseBaseEntity category() {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<StudentWitnessCategoryResp> result = studentWitnessService.categoryList();
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("list")
    @ApiOperation("前端接口-学员见证-列表")
    public ResponseBaseEntity list(@RequestBody StudentWitnessListReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<StudentWitnessListResp> result = studentWitnessService.list(req.getCategoryId());
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("like")
    @ApiOperation("前端接口-学员见证-点赞")
    public ResponseBaseEntity like(@RequestBody StudentWitnessLikeReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            boolean result = studentWitnessService.like(req.getWitnessId(), ATMRContext.getContext().getOpenId(),req.isCancelLike());
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("listPage")
    @ApiOperation("前端接口-学员见证-列表带分页")
    public ResponseBaseEntity listPage(@RequestBody StudentWitnessListReq req) {
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            PageData<StudentWitnessListResp> result = studentWitnessService.listPage(req);
            response.setBody(result);
        } catch (WeChatException e) {
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        } catch (Exception e) {
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
